package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * @author gengjian
     * @date 2024/12/15 17:07:31
     * 根据id获取公寓信息详情
     */
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //根据公寓id获取公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //根据公寓id获取图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(ItemType.APARTMENT,id);
        //根据公寓id获取标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListLabelInfoByApartmentId(id);
        //根据公寓id获取配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListFacilityInfoByApartmentId(id);
        //根据公寓id找出房间租金最小值
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);
        //获取公寓是否被删除
        int isDeleted = apartmentInfo.getIsDeleted();

        //封装到vo
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setIsDelete(isDeleted == 1);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //返回
        return apartmentDetailVo;
    }

    /**
     * @author gengjian
     * @date 2024/12/15 18:57:49
     * 根据房间信息获取所属公寓id，再通过公寓id获取公寓信息
     */
    @Override
    public ApartmentItemVo getApartmentItemVo(Long id) {
        //根据公寓id获取公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //根据公寓id获取图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(ItemType.APARTMENT,id);
        //根据公寓id获取标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListLabelInfoByApartmentId(id);
        //根据公寓id找出房间租金最小值
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);
        //创建vo对象
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        //封装
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setMinRent(minRent);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphVoList);

        return apartmentItemVo;
    }
}




